package com.promis.widget.dialog;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.promis.util.ScreenUtils;
import com.promis.widget.R;

public abstract class ChooseListDialog extends Builder {
	
	private TextView titleView;

	public ChooseListDialog(final Activity activity, String[] list) {
		this(activity, R.style.Theme_AlertDialog, list);
	}

	public ChooseListDialog(final Activity activity, int theme, final String[] list) {
		super(activity, theme);
		
		ViewGroup.LayoutParams wrapHeightParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		int titleHeight = ScreenUtils.dp2px(getContext(), 60);
		
		LinearLayout titleLayout = new LinearLayout(activity);
		titleLayout.setLayoutParams(wrapHeightParams);
		titleView = new TextView(activity);
		LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, titleHeight);
		titleParams.setMargins(30, 0, 30, 0);
		titleView.setLayoutParams(titleParams);
		titleView.setPadding(30, 30, 30, 30);
		titleView.setBackgroundResource(R.drawable.layer_stroke_gray_bottom);
		titleView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
		titleLayout.addView(titleView);
		setCustomTitle(titleLayout);
		
		setItems(list, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				onSelectedItem(dialog, which);
			}
		});
	}

	@Override
	public Builder setTitle(CharSequence title) {
		// TODO Auto-generated method stub
		titleView.setText(title);
		return super.setTitle(title);
	}
	
	@Override
	public Builder setTitle(int titleResId) {
		// TODO Auto-generated method stub
		titleView.setText(titleResId);
		return super.setTitle(titleResId);
	}
	
	public TextView getTitleView() {
		return titleView;
	}
	
	public abstract void onSelectedItem(DialogInterface dialog, int which);
}
